<template>
	<view class="container">
		<TopBtn :title="'编辑资料'"></TopBtn>
		<view class="info">
			<view class="title">
				个人信息
			</view>
			<view class="currency_card pb_1">
				<uni-forms ref="baseForm" :label-width="180" :modelValue="baseFormData">
					<uni-forms-item label="姓名：">
						<uni-easyinput v-model="baseFormData.name" disabled :clearable="false" placeholder="请输入您的联系方式" />
					</uni-forms-item>
					<uni-forms-item label="身份证号：">
						<uni-easyinput v-model="baseFormData.idCardDesensitize" disabled :clearable="false"
							placeholder="请输入您的联系方式" />
					</uni-forms-item>
					<uni-forms-item label="性别：">
						<uni-easyinput v-model="baseFormData.sexName" disabled :clearable="false" placeholder="请输入您的联系方式" />
					</uni-forms-item>
					<uni-forms-item label="民族：" required>
						<RadioPicker2 v-model="baseFormData.nation" :inputName="baseFormData.nation" :Radio="'nation'">
						</RadioPicker2>
					</uni-forms-item>
					<uni-forms-item label="最高学历：" required>
						<RadioPicker2 v-model="baseFormData.education" :inputName="baseFormData.education"
							:Radio="'user_education'">
						</RadioPicker2>
					</uni-forms-item>
					<uni-forms-item label="最高学历取得日期：" required>
						<picker mode="date" :value="baseFormData.educationDate" @change="bindDateChange">
							<view class="uni-input">
								<text v-if="baseFormData.educationDate">{{ baseFormData.educationDate }}</text>
								<text v-else class="placeholder">未填写</text>
								<uni-icons type="right" color="#999" size="16"></uni-icons>
							</view>
						</picker>
					</uni-forms-item>
					<uni-forms-item label="参加工作日期：" required>
						<picker mode="date" :value="baseFormData.staffBirthday" @change="bindDateChange2">
							<view class="uni-input">
								<text v-if="baseFormData.staffBirthday">{{ baseFormData.staffBirthday }}</text>
								<text v-else class="placeholder">未填写</text>
								<uni-icons type="right" color="#999" size="16"></uni-icons>
							</view>
						</picker>
					</uni-forms-item>
					<uni-forms-item label="临床工作年限：" required>
						<view class="uni-input">
							<uni-easyinput v-model="baseFormData.workYears" maxlength="2" :clearable="false" type="number"
								placeholder="请输入年限"></uni-easyinput>年
						</view>
						</RadioPicker2>
					</uni-forms-item>
					<!-- <uni-forms-item label="就职医院：" required >
						<RadioPicker v-model="baseFormData.employmentHospital" :inputName="baseFormData.employmentHospital"
							:Radio="'matchIncome'" :range="HospitalList"></RadioPicker>
					</uni-forms-item> -->
					<uni-forms-item label="就职医院：" required v-if="baseFormData.role!=2">
						<uni-combox class="nameinput" :candidates="HospitalList" placeholder="请输入就职医院"
							v-model="customName" @input="comboxINput()"></uni-combox>
					</uni-forms-item>
					<uni-forms-item label="医院地址：" required v-if="!HospitalList.includes(customName)&&baseFormData.role!=2">
						<addressRadioPicker v-model="baseFormData.areaId" :inputName="baseFormData.areaId"></addressRadioPicker>
					</uni-forms-item>
					<uni-forms-item label="所属科室：" required>
						<MulticolumnRadioPicker v-model="baseFormData.departId" :range="DepartManagementTree"
							:inputName="baseFormData.departId"></MulticolumnRadioPicker>
					</uni-forms-item>
					<uni-forms-item label="诊疗科目：" required>
						<RadioPicker v-model="baseFormData.diagnosisId" :inputName="baseFormData.diagnosisId" :Radio="'matchIncome'"
							:range="DiagnosisSubject"></RadioPicker>
					</uni-forms-item>
					<uni-forms-item label="职称：" required>
						<RadioPicker v-model="baseFormData.titleId" :inputName="baseFormData.titleId" :Radio="'matchIncome'"
							:range="TitleInfo"></RadioPicker>
					</uni-forms-item>
					<uni-forms-item label="擅长疾病：" v-if="baseFormData.role!=2">
						<MulticolumnRadioPicker v-model="baseFormData.proficientDiseases" :range="DiseaseTree"
							:inputName="baseFormData.proficientDiseases"></MulticolumnRadioPicker>
					</uni-forms-item>
				</uni-forms>
			</view>
			<view class="title">
				上传资料
			</view>
			<view class="currency_card pb_30">
				<view class="imgTitle mb_17">
					<text></text> 医师头像
				</view>
				<view class="red fs_22">
					请近期拍摄的免冠（不戴帽子）正面照，建议上传1：1比例的照片，图片大小不超过50K
				</view>
				<view class="image">
					<addImg v-model="avatarList" :limit="1" :inputName="avatarList"></addImg>
				</view>
			</view>
			<view class="currency_card pb_30">
				<view class="imgTitle mb_17">
					<text></text> 工作证/胸牌(包含姓名)
				</view>
				<view class="red fs_22">
					或上传以下任意一种：医院HIS系统工作站截图 (包含姓名) ；或单位出具的在职证明；或医院官网个人信息截图；或劳动合同。
				</view>
				<view class="image">
					<addImg v-model="baseFormData.workPermit" :inputName="baseFormData.workPermit"></addImg>
				</view>
			</view>
			<view class="currency_card pb_30 mt_30">
				<view class="imgTitle mb_17">
					<text></text> {{baseFormData.role==1?'医师':'药师'}}执业证
				</view>
				<view class="red fs_22">
					需完整上传个人信息和执业地点变更记录；如无纸质证件，可登录医师电子化注册信息系统(https://cndocsys.cn) ，下载电子证件 </view>
				<view class="image">
					<addImg v-model="baseFormData.practiceCertificate" :inputName="baseFormData.practiceCertificate"></addImg>
				</view>
			</view>
			<view class="currency_card pb_30 mt_30" v-if="baseFormData.role!=2">
				<view class="imgTitle mb_17">
					<text></text> 医师资格证
				</view>
				<view class="red fs_22">
					需完整上传个人信息和执业地点变更记录；如无纸质证件，可登录医师电子化注册信息系统(https://cndocsys.cn) ，下载电子证件 </view>
				<view class="image">
					<addImg v-model="baseFormData.qualificationCertificate" :inputName="baseFormData.qualificationCertificate">
					</addImg>
				</view>
			</view>
		</view>
		<view class="info mt_30">
			<view class="warning currency_card">
				<view class="title flex-center">
					<image src="@/static/CommonTools/warning.png" mode=""></image> 温馨提示
				</view>
				<view class="text">
					提交资料后需等待审核，审核通过后才可正常使用APP功能审核结果会以短信形式告知。
				</view>
			</view>
		</view>
		<view class="addBtn" @click="certification()">
			发布
		</view>
		<view class="kong">

		</view>
	</view>
</template>

<script>
	import {
		getDoctorInfo,
		getHospitalNameList,
		getDepartManagementTree,
		getDiseaseTree,
		getTitleInfo,
		updateDoctorInfo,
		getDiagnosisSubject,
		dictDataType,
	} from '@/api/user'
	export default {
		data() {
			return {
				baseFormData: {
					titleId: "",
					employmentHospital: "",
					proficientDiseases: "",
					customName: ""
				},
				HospitalList: [],
				DepartManagementTree: [],
				TitleInfo: [],
				DiseaseTree: [],
				DiagnosisSubject: [],
				avatarList: "",
				Dictionaries: {
					nationList: [],
					user_educationList: []
				},
				city: "",
				customName: ""
			}
		},
		onLoad() {
			this.getDoctorInfo()
			this.getHospitalNameList()
			this.getDepartManagementTree()
			// this.getTitleInfo()
			this.getDiseaseTree()
			this.getDiagnosisSubject()
		},
		methods: {
			comboxINput(e){
				console.log(111)
				this.baseFormData.areaId=[]
			},
			showFullImage(url, list) {
				uni.previewImage({
					current: url, // 当前显示的图片链接
					urls: list.split(','), // 所有图片链接列表
				})
			},
			certification(ref) {
				// this.$refs[ref].validate().then(res => {
				this.baseFormData.customName = this.customName
				if (!this.baseFormData.nation) {
					uni.showToast({
						title: '请选择民族',
						duration: 2000,
						icon: 'none',
					})
					return
				}
				if (!this.baseFormData.education) {
					uni.showToast({
						title: '请选择学历',
						duration: 2000,
						icon: 'none',
					})
					return
				}
				if (!this.baseFormData.educationDate) {
					uni.showToast({
						title: '请选择学历获取日期',
						duration: 2000,
						icon: 'none',
					})
					return
				}
				if (!this.baseFormData.staffBirthday) {
					uni.showToast({
						title: '请选择参加工作日期',
						duration: 2000,
						icon: 'none',
					})
					return
				}
				if (!this.baseFormData.workYears) {
					uni.showToast({
						title: '请选择临床工作年限',
						duration: 2000,
						icon: 'none',
					})
					return
				}


				if (!this.baseFormData.customName && this.baseFormData.role != 2) {
					uni.showToast({
						title: '请选择就职医院',
						duration: 2000,
						icon: 'none',
					})
					return
				}
				console.log(this.HospitalList.includes(this.baseFormData.customName), this.baseFormData.role)
				if (!this.HospitalList.includes(this.baseFormData.customName) && this.baseFormData.role != 2) {
					if (!this.baseFormData.areaId) {
						uni.showToast({
							title: '请选择就职医院地址',
							duration: 2000,
							icon: 'none',
						})
						return
					}
				}
				if (!this.baseFormData.departId) {
					uni.showToast({
						title: '请选择科室',
						duration: 2000,
						icon: 'none',
					})
					return
				}
				if (!this.baseFormData.diagnosisId) {
					uni.showToast({
						title: '请选择诊疗科目',
						duration: 2000,
						icon: 'none',
					})
					return
				}
				if (!this.baseFormData.titleId) {
					uni.showToast({
						title: '请选择职称',
						duration: 2000,
						icon: 'none',
					})
					return
				}
				if (this.avatarList && JSON.parse(this.avatarList)[0]) {
					this.baseFormData.avatar = JSON.parse(this.avatarList)[0].url
				} else {
					uni.showToast({
						title: '请上传头像',
						duration: 2000,
						icon: 'none',
					})
					return
				}
				if (this.baseFormData.workPermit && JSON.parse(this.baseFormData.workPermit) && !JSON.parse(this.baseFormData
						.workPermit)[0]) {
					uni.showToast({
						title: '请上传工作证/胸拍',
						duration: 2000,
						icon: 'none',
					})
					return
				}
				if (this.baseFormData.practiceCertificate && !JSON.parse(this.baseFormData.practiceCertificate)[0]) {
					uni.showToast({
						title: '医师执业证',
						duration: 2000,
						icon: 'none',
					})
					return
				}
				if (this.baseFormData.qualificationCertificate && !JSON.parse(this.baseFormData.qualificationCertificate)[0] &&
					this.baseFormData.role != 2) {
					uni.showToast({
						title: '医生资格证',
						duration: 2000,
						icon: 'none',
					})
					return
				}




				updateDoctorInfo(this.baseFormData).then(res => {
					uni.showToast({
						title: '发布成功，请等待审核',
						icon: 'none',
						duration: 2000,
						mask: true
					})
					setTimeout(() => {
						//需要定时执行的代码
						if (this.baseFormData.role == 2) {
							uni.reLaunch({
								url: `/pages/Pharmacist/Pharmacist`,
							})
						} else {
							uni.switchTab({
								url: '/pages/index/index',
							})
						}

					}, 2000)
				})
				// }).catch(err => {
				// 	console.log('表单错误信息：', err);
				// })
			},
			bindDateChange(e) {
				this.baseFormData.educationDate = e.detail.value
			},
			bindDateChange2(e) {
				this.baseFormData.staffBirthday = e.detail.value
			},
			getHospitalNameList() {
				getHospitalNameList().then(res => {
					this.HospitalList = res.data
				})
			},
			getTitleInfo(e) {
				getTitleInfo({
					typeId: e
				}).then(res => {
					this.TitleInfo = res.data
				})
			},
			getDiagnosisSubject() {
				getDiagnosisSubject().then(res => {
					this.DiagnosisSubject = res.data
				})
			},
			getDepartManagementTree() {
				getDepartManagementTree().then(res => {
					this.DepartManagementTree = res.data
				})
			},
			getDoctorInfo() {
				getDoctorInfo().then(res => {
					this.baseFormData = res.data
					if (res.data.hospitalName) {
						this.baseFormData.customName = res.data.hospitalName
						this.customName = res.data.hospitalName
					}

					if (res.data.avatar) {
						var avatar = [{
							url: res.data.avatar,
							path: res.data.avatar
						}]
						this.avatarList = JSON.stringify(avatar)
					}

					this.getTitleInfo(res.data.role)
				})
			},
			getDiseaseTree() {
				getDiseaseTree().then(res => {
					this.DiseaseTree = res.data
				})
			},
		}
	}
</script>

<style lang="less" scoped>
	/deep/.is-disabled {
		background-color: #fff !important;
		color: #333333;
	}

	/deep/.uni-input {
		font-size: 28rpx !important;
	}

	.nameinput {
		border: none !important;

		/deep/.uni-input-placeholder {
			text-align: right;
		}

		/deep/.uni-input-input {
			text-align: right;
		}

		/deep/.uni-icons {
			display: none;
		}
	}

	.uni-input {
		display: flex;
		justify-content: right;
		align-items: center;
		height: 100%;
		font-size: 30rpx;
		font-family: PingFang SC;
		font-weight: 500;

		.placeholder {
			color: #999;
		}
	}


	.warning {
		padding: 30rpx;

		image {
			width: 30rpx;
			height: 30rpx;
			margin-right: 15rpx;
		}

		font-weight: 500;
		font-size: 28rpx;
		color: #333333;

		.text {
			color: #999;
		}
	}

	.kong {
		height: 260rpx;
	}

	.addBtn {
		width: 649rpx;
		height: 100rpx;
		background: #3B6CFE;
		border-radius: 50rpx;
		font-weight: 500;
		font-size: 28rpx;
		color: #FFFFFF;
		text-align: center;
		line-height: 100rpx;
		position: fixed;
		bottom: 99rpx;
		left: 50%;
		transform: translateX(-50%);
		z-index: 3;
	}

	.currency_card {
		.imgTitle {
			display: flex;
			align-items: center;
			font-weight: 500;
			font-size: 28rpx;
			color: #333333;

			image {
				width: 27rpx;
				height: 27rpx;
				margin-left: 19rpx;

			}

			text {
				margin-right: 16rpx;
				display: inline-block;
				width: 10rpx;
				height: 10rpx;
				background: #333333;
				border-radius: 50%;
			}
		}
	}

	.info {
		padding: 0 30rpx;

		/deep/.is-input-border {
			border: none;
		}

		/deep/.uni-forms-item {
			margin-bottom: 10rpx;
		}

		/deep/.uni-easyinput__content-input {
			text-align: right;
			padding-right: 10rpx;
		}

		.title {
			font-weight: bold;
			font-size: 32rpx;
			color: #333333;
			margin: 30rpx 0;

		}

	}
</style>